/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.ISubjectiveInfoDAO;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.entity.SubjectiveInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Long.
 * @version 1.0
 * @className SubjectiveInfoDAOImpl
 * @since 2016年10月30日
 */
public class SubjectiveInfoDAOImpl implements ISubjectiveInfoDAO {

    @Override
    public int addSubjectiveInfo(SubjectiveInfo subjective) {
        String sql = "insert into subjectiveinfo values(null,?,?,?)";
        Object[] parameters = {subjective.getQuestion(), subjective.getAnswer(),
                subjective.getCourse().getCourseId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public List<Map<String, Object>> getSubjective() {
        return JdbcUtils.executeQuery("select * from subjectiveinfo");
    }

    @Override
    public int updateSubjective(SubjectiveInfo subjective) {
        String sql = "update subjectiveinfo set question=?,answer=?,courseid=? where subid=?";
        Object[] parameters = {subjective.getQuestion(), subjective.getAnswer(), subjective.getCourse().getCourseId(),
                subjective.getSubId()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int deleteSubjective(String subId) {
        return JdbcUtils.executeUpdate("delete from subjectiveinfo where subid=?", subId);
    }

    @Override
    public List<Map<String, Object>> getSubjectiveByPage(PageBean page) {
        String sql = "select * from subjectiveinfo limit ?,?";
        // 获取每页显示记录数，当前页
        Integer pageSize = page.getPageSize();
        Integer currentPage = page.getCurrentPage();
        // 计算每页显示第一条记录索引
        Integer startIndex = (currentPage - 1) * pageSize;

        Object[] parameters = {startIndex, pageSize};
        return JdbcUtils.executeQuery(sql, parameters);
    }

    @Override
    public int getTotalCount() {
        String sql = "select * from subjectiveinfo";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql);
        return CollectionUtils.isEmptyForList(list) ? list.size() : 0;
    }

    @Override
    public Map<String, Object> getSubjectiveInfoById(String subId, String courseId) {
        String sql = "select * from subjectiveInfo where subId= ? and courseId = ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, subId, courseId);

        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<Map<String, Object>> getRandomSubjectiveInfoById(String number, String courseId) {
        String sql = "select * from subjectiveInfo where courseId = ? order by rand() limit ?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, courseId, Integer.parseInt(number));

        return list;
    }

    /* (non-Javadoc)
     * @see com.ssyt.examinationsystem.dao.ISubjectiveInfoDAO#getSubById(java.lang.String)
     */
    @Override
    public Map<String, Object> getSubById(String subId) {
        List<Map<String, Object>> list = JdbcUtils.executeQuery("select * from subjectiveInfo where subId=?", subId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

}
